import { getCaptcha, getMyData, postUserInfo } from "@/api/common";
import dayjs from "dayjs";
import { useAnimationStore, useMainStore, useMusicStore } from "@/store/index";
export function getTime() {
  return dayjs().format("YYYY-MM-DD");
}
type responseType = { code: number; data: any; message: string };

declare global {
  interface Window {
    NavigatorChannel: { postMessage: (message: string) => void };
    FunctionChannel: { postMessage: (message: string) => void };
    getUserCallback: (res: responseType) => void;
    getCurrentStudentCallback: (res: responseType) => void;
    getSafeAreaInsetsCallback: (res: responseType) => void;
    setBackgroundMusic: (isPlay: boolean) => void;
    setAnimationFunction: (list: string[] | number[]) => void;
  }
}

export function skipToApp(
  route: string,
  args?: { [key: string]: string },
  params?: { [key: string]: string }
) {
  return new Promise<void>((resolve, reject) => {
    try {
      window.NavigatorChannel.postMessage(
        JSON.stringify({
          name: "pushNamed",
          route,
          arguments: args,
          parameters: params,
        })
      );
    } catch (e) {
      reject(e);
    }
  });
}

export function getUserInfo(arg?: { [key: string]: string }) {
  return new Promise<void | responseType>(async (resolve, reject) => {
    try {
      window.FunctionChannel.postMessage(
        JSON.stringify({
          name: "getUser",
          arguments: arg,
          callback: "getUserCallback",
        })
      );
    } catch (e) {
      const data = {
        captchaCode: "123456",
        captchaUuid: "",
        phone: "15136773192",
      };
      const captchaRes = await getCaptcha();
      data.captchaUuid = captchaRes.data.captchaUuid;
      data.captchaCode = captchaRes.data.captchaText;
      const userInfoRes = await postUserInfo(data);
      const mainStore = useMainStore();
      mainStore.setUserInfo(userInfoRes.data);
      sessionStorage.setItem("userInfo", JSON.stringify(userInfoRes.data));
      reject(e);
    }
  });
}

export function getCurrentStudent(arg?: { [key: string]: string }) {
  return new Promise<void | responseType>(async (resolve, reject) => {
    try {
      window.FunctionChannel.postMessage(
        JSON.stringify({
          name: "getCurrentStudent",
          arguments: arg,
          callback: "getCurrentStudentCallback",
        })
      );
    } catch (e) {
      getMyData().then((res) => {
        const mainStore = useMainStore();
        mainStore.setStudentInfo(res.data.studentList[0]);
      });
      reject(e);
    }
  });
}

export function getSafeAreaInsets(arg?: { [key: string]: string }) {
  return new Promise<void | responseType>(async (resolve, reject) => {
    try {
      window.FunctionChannel.postMessage(
        JSON.stringify({
          name: "getSafeAreaInsets",
          arguments: arg,
          callback: "getSafeAreaInsetsCallback",
        })
      );
    } catch (e) {
      reject(e);
    }
  });
}

export function RegistrationFunction() {
  window.setAnimationFunction = function (list: string[] | number[]) {
    const animationFun = useAnimationStore();
    animationFun.setList(list);
  };
  window.setBackgroundMusic = function (isPlay: boolean) {
    const musicFun = useMusicStore();
    musicFun.setIsPlay(isPlay);
  };
  window.getSafeAreaInsetsCallback = function (res: responseType) {
    document
      .getElementById("app")
      ?.style.setProperty("--dynamic-top", `${res.data.top}px`);
    document
      .getElementById("app")
      ?.style.setProperty("--dynamic-bottom", `${res.data.bottom}px`);
  };
  window.getCurrentStudentCallback = function (res: responseType) {
    console.log("🚀 ~ RegistrationFunction ~ res.data:", res.data);
    const mainStore = useMainStore();
    mainStore.setStudentInfo(res.data);
    console.log(
      "🚀 ~ RegistrationFunction ~ mainStore.setStudentInfo(res.data);:",
      mainStore.studentInfo
    );
  };
  window.getUserCallback = function (res: responseType) {
    console.log("🚀 ~ RegistrationFunction ~ res.data:", res.data);
    const mainStore = useMainStore();
    mainStore.setUserInfo(res.data);
    console.log(
      "🚀 ~ RegistrationFunction ~ mainStore.setUserInfo(res.data):",
      mainStore.userInfo
    );
    sessionStorage.setItem("userInfo", JSON.stringify(res.data));
  };
}
